package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-05-17
 * Time: 7:59
 */

//Cloneable和深拷贝
class Money implements Cloneable{
    public double money = 99.99;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{
    public int age = 10;
    public Money m = new Money();
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}
public class Test{

    public static void main4(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person();
        Person person2 = (Person)person1.clone();
        System.out.println(person1.m.money);
        System.out.println(person2.m.money);
        person2.m.money = 9.9;
        System.out.println("+====================+");
        System.out.println(person1.m.money);
        System.out.println(person2.m.money);
    }
    public static void main3(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person();
        Money tmp = new Money();
        tmp = (Money)person1.m.clone();
        Person person2 = (Person)person1.clone();
        person2.m = tmp;
        System.out.println(person1.m.money);
        System.out.println(person2.m.money);
        person2.m.money = 9.9;
        System.out.println("+======================+");
        System.out.println(person1.m.money);
        System.out.println(person2.m.money);
    }
    public void func() throws CloneNotSupportedException {
        Person person1 = new Person();
        Person person2 = (Person)super.clone();
        System.out.println(person1.age);
        System.out.println(person2.age);
    }

    public static void main2(String[] args) throws CloneNotSupportedException {
        Test test = new Test();
        test.func();
    }
    public static void main1(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person();
        Person person2 = (Person)person1.clone();
        System.out.println(person1.age);
        System.out.println(person2.age);
        person2.age = 99;
        System.out.println("+====================+");
        System.out.println(person1.age);
        System.out.println(person2.age);
    }
}
